package com.wgts.wgts.mvp.view.activity.awardmore;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.eventbus.OrderFragmentEvenBus;
import com.wgts.wgts.control.extrusionecycleview.PowerfulStickyDecoration;
import com.wgts.wgts.control.extrusionecycleview.listener.PowerGroupListener;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerJoinActionActivityComponent;
import com.wgts.wgts.dagger.module.JoinActionActivityModule;
import com.wgts.wgts.mvp.presenter.IJoinActionActivityPresenter;
import com.wgts.wgts.mvp.view.IJoinActionActivityView;
import com.wgts.wgts.mvp.view.activity.guessing.GuessingOrderParentAdapter;
import com.wgts.wgts.netmodle.resp.GuessingAndAwardOrderEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.badgeview.DisplayUtil;

public class AwardJoinActionActivity extends BaseActivity implements IJoinActionActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;

    @Inject
    IJoinActionActivityPresenter presenter;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.linear_noting)
    LinearLayout linearNoting;

    private List<GuessingAndAwardOrderEntity> entityList;

    private MyAwardOrderParentAdapter adapter;

    public static final String TYPE = "type";

    public static final String GUESS = "GUESS";

    public static final String MORE_MORE = "MORE_MORE";

    private String type;

    @Override
    public void init() {
        DaggerJoinActionActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .joinActionActivityModule(new JoinActionActivityModule(this))
                .build()
                .inject(this);
        entityList = new ArrayList<>();
    }

    @Override
    public void onCreate(Bundle saveInstanceBudle) {
        super.onCreate(saveInstanceBudle);
        setContentView(R.layout.join_action_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        load();
        includeTitle.setText("我参与的");
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        initOrderRecycle();
        type = getIntent().getStringExtra(TYPE);
        switch (type) {
            case GUESS:
                presenter.initJoinActivityList(1);
                break;
            case MORE_MORE:
                presenter.initJoinActivityList(0);
                break;
        }

        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setEnableRefresh(true);
    }

    @Override
    public void initActionList(List<GuessingAndAwardOrderEntity> entities, boolean noMore) {
        refreshLayout.finishRefresh();
        if(entities.size() == 0){
            linearNoting.setVisibility(View.VISIBLE);
        }else{
            linearNoting.setVisibility(View.GONE);
        }

        entityList.clear();
        entityList.addAll(entities);
        adapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreActionList(List<GuessingAndAwardOrderEntity> entities, boolean noMore) {
        refreshLayout.finishLoadmore();
        entityList.addAll(entities);
        adapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onError(int status, String msg) {

    }

    private void initOrderRecycle() {
        //我参与的订单recycleView
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        PowerGroupListener listener = new PowerGroupListener() {
            @Override
            public String getGroupName(int position) {
                if (entityList.size() > position){
                    return entityList.get(position).date;
                }
                return null;
            }

            @Override
            public View getGroupView(int position) {
                //获取自定定义的组View
                View view = getLayoutInflater().inflate(R.layout.extrusion_item_head, null, false);
                ((TextView) view.findViewById(R.id.extrusion_head_text)).setText(entityList.get(position).date);
                return view;
            }
        };
        PowerfulStickyDecoration decoration = PowerfulStickyDecoration.Builder
                .init(listener)
                .setGroupBackground(ContextCompat.getColor(this, R.color.award_bg))
                .setGroupHeight(DisplayUtil.dp2px(this, 44))
                //重置span（注意：使用GridLayoutManager时必须调用）
                //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
                .build();
        recycleView.addItemDecoration(decoration);
        adapter = new MyAwardOrderParentAdapter(this, entityList, 1, 1);
        recycleView.setAdapter(adapter);
    }

    @OnClick(R.id.back_btn_image)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OrderFragmentEvenBus event) {
        switch (type) {
            case GUESS:
                presenter.initJoinActivityList(1);
                break;
            case MORE_MORE:
                presenter.initJoinActivityList(0);
                break;
        }
    }

    private void load(){

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMoreJoinActivityList();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                switch (type) {
                    case GUESS:
                        presenter.initJoinActivityList(1);
                        break;
                    case MORE_MORE:
                        presenter.initJoinActivityList(0);
                        break;
                }
            }
        });
    }
}
